<template>
  <div class="charts-demo">
    <div class="example-section">
      <h3 class="section-title">基础图表</h3>
      <p class="section-description">展示各种基础图表组件，包括柱状图、折线图、饼图等</p>

      <div class="charts-grid">
        <div class="chart-card">
          <div class="chart-header">
            <h4>月度销售额</h4>
            <span class="chart-period">2024年</span>
          </div>
          <div class="chart-content">
            <div class="chart-placeholder">
              <bar-chart-outlined class="chart-icon" />
              <div class="chart-text">柱状图示例</div>
              <div class="chart-data">
                <div class="data-item">
                  <span class="data-label">1月</span>
                  <div class="data-bar" style="width: 60%"></div>
                  <span class="data-value">¥120,000</span>
                </div>
                <div class="data-item">
                  <span class="data-label">2月</span>
                  <div class="data-bar" style="width: 80%"></div>
                  <span class="data-value">¥160,000</span>
                </div>
                <div class="data-item">
                  <span class="data-label">3月</span>
                  <div class="data-bar" style="width: 100%"></div>
                  <span class="data-value">¥200,000</span>
                </div>
                <div class="data-item">
                  <span class="data-label">4月</span>
                  <div class="data-bar" style="width: 75%"></div>
                  <span class="data-value">¥150,000</span>
                </div>
              </div>
            </div>
          </div>
        </div>

        <div class="chart-card">
          <div class="chart-header">
            <h4>用户增长趋势</h4>
            <span class="chart-period">近30天</span>
          </div>
          <div class="chart-content">
            <div class="chart-placeholder">
              <line-chart-outlined class="chart-icon" />
              <div class="chart-text">折线图示例</div>
              <div class="trend-chart">
                <svg width="100%" height="120" viewBox="0 0 300 120">
                  <polyline
                    points="20,100 60,80 100,60 140,40 180,50 220,30 260,20"
                    fill="none"
                    stroke="#1890ff"
                    stroke-width="2"
                  />
                  <circle cx="20" cy="100" r="3" fill="#1890ff" />
                  <circle cx="60" cy="80" r="3" fill="#1890ff" />
                  <circle cx="100" cy="60" r="3" fill="#1890ff" />
                  <circle cx="140" cy="40" r="3" fill="#1890ff" />
                  <circle cx="180" cy="50" r="3" fill="#1890ff" />
                  <circle cx="220" cy="30" r="3" fill="#1890ff" />
                  <circle cx="260" cy="20" r="3" fill="#1890ff" />
                </svg>
              </div>
            </div>
          </div>
        </div>

        <div class="chart-card">
          <div class="chart-header">
            <h4>设备类型分布</h4>
            <span class="chart-period">本月</span>
          </div>
          <div class="chart-content">
            <div class="chart-placeholder">
              <pie-chart-outlined class="chart-icon" />
              <div class="chart-text">饼图示例</div>
              <div class="pie-chart">
                <div class="pie-item">
                  <span class="pie-dot" style="background: #1890ff;"></span>
                  <span class="pie-label">移动端</span>
                  <span class="pie-value">65%</span>
                </div>
                <div class="pie-item">
                  <span class="pie-dot" style="background: #52c41a;"></span>
                  <span class="pie-label">桌面端</span>
                  <span class="pie-value">25%</span>
                </div>
                <div class="pie-item">
                  <span class="pie-dot" style="background: #faad14;"></span>
                  <span class="pie-label">平板端</span>
                  <span class="pie-value">10%</span>
                </div>
              </div>
            </div>
          </div>
        </div>

        <div class="chart-card">
          <div class="chart-header">
            <h4>实时数据监控</h4>
            <span class="chart-period">实时</span>
          </div>
          <div class="chart-content">
            <div class="chart-placeholder">
              <area-chart-outlined class="chart-icon" />
              <div class="chart-text">面积图示例</div>
              <div class="metrics-grid">
                <div class="metric-item">
                  <div class="metric-value">{{ formatNumber(8960) }}</div>
                  <div class="metric-label">在线用户</div>
                  <div class="metric-trend positive">+12.5%</div>
                </div>
                <div class="metric-item">
                  <div class="metric-value">{{ formatNumber(2580) }}</div>
                  <div class="metric-label">活跃会话</div>
                  <div class="metric-trend positive">+8.2%</div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="example-section">
      <h3 class="section-title">仪表盘组件</h3>
      <p class="section-description">展示仪表盘样式的数据可视化组件</p>

      <div class="dashboard-grid">
        <div class="dashboard-card">
          <div class="dashboard-header">
            <h4>系统性能监控</h4>
            <a-tag color="success">正常</a-tag>
          </div>
          <div class="dashboard-content">
            <div class="gauge-container">
              <div class="gauge-item">
                <div class="gauge-title">CPU使用率</div>
                <div class="gauge-value">45%</div>
                <a-progress
                  type="circle"
                  :percent="45"
                  :width="80"
                  stroke-color="#52c41a"
                />
              </div>
              <div class="gauge-item">
                <div class="gauge-title">内存使用率</div>
                <div class="gauge-value">68%</div>
                <a-progress
                  type="circle"
                  :percent="68"
                  :width="80"
                  stroke-color="#faad14"
                />
              </div>
              <div class="gauge-item">
                <div class="gauge-title">磁盘使用率</div>
                <div class="gauge-value">32%</div>
                <a-progress
                  type="circle"
                  :percent="32"
                  :width="80"
                  stroke-color="#1890ff"
                />
              </div>
            </div>
          </div>
        </div>

        <div class="dashboard-card">
          <div class="dashboard-header">
            <h4>业务数据概览</h4>
            <a-dropdown>
              <template #overlay>
                <a-menu>
                  <a-menu-item key="1">今日</a-menu-item>
                  <a-menu-item key="2">本周</a-menu-item>
                  <a-menu-item key="3">本月</a-menu-item>
                </a-menu>
              </template>
              <more-outlined />
            </a-dropdown>
          </div>
          <div class="dashboard-content">
            <div class="kpi-grid">
              <div class="kpi-item">
                <div class="kpi-icon">
                  <user-outlined />
                </div>
                <div class="kpi-content">
                  <div class="kpi-value">{{ formatNumber(12580) }}</div>
                  <div class="kpi-label">总用户数</div>
                  <div class="kpi-change positive">+12.5%</div>
                </div>
              </div>
              <div class="kpi-item">
                <div class="kpi-icon">
                  <transaction-outlined />
                </div>
                <div class="kpi-content">
                  <div class="kpi-value">¥{{ formatNumber(2580000) }}</div>
                  <div class="kpi-label">总交易额</div>
                  <div class="kpi-change positive">+18.2%</div>
                </div>
              </div>
              <div class="kpi-item">
                <div class="kpi-icon">
                  <shop-outlined />
                </div>
                <div class="kpi-content">
                  <div class="kpi-value">{{ formatNumber(1580) }}</div>
                  <div class="kpi-label">活跃商户</div>
                  <div class="kpi-change negative">-2.1%</div>
                </div>
              </div>
              <div class="kpi-item">
                <div class="kpi-icon">
                  <file-text-outlined />
                </div>
                <div class="kpi-content">
                  <div class="kpi-value">{{ formatNumber(8960) }}</div>
                  <div class="kpi-label">今日订单</div>
                  <div class="kpi-change positive">+15.8%</div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="example-section">
      <h3 class="section-title">组件代码示例</h3>
      <p class="section-description">图表组件的使用方法</p>

      <a-collapse>
        <a-collapse-panel key="1" header="基础图表代码">
          <pre class="code-block"><code>&lt;template&gt;
  &lt;div class="chart-card"&gt;
    &lt;div class="chart-header"&gt;
      &lt;h4&gt;月度销售额&lt;/h4&gt;
      &lt;span class="chart-period"&gt;2024年&lt;/span&gt;
    &lt;/div&gt;
    &lt;div class="chart-content"&gt;
      &lt;!-- 图表内容 --&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/template&gt;</code></pre>
        </a-collapse-panel>

        <a-collapse-panel key="2" header="仪表盘组件代码">
          <pre class="code-block"><code>&lt;template&gt;
  &lt;div class="dashboard-card"&gt;
    &lt;div class="dashboard-header"&gt;
      &lt;h4&gt;系统性能监控&lt;/h4&gt;
      &lt;a-tag color="success"&gt;正常&lt;/a-tag&gt;
    &lt;/div&gt;
    &lt;div class="dashboard-content"&gt;
      &lt;a-progress
        type="circle"
        :percent="45"
        :width="80"
        stroke-color="#52c41a"
      /&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/template&gt;</code></pre>
        </a-collapse-panel>
      </a-collapse>
    </div>
  </div>
</template>

<script setup lang="ts">
import {
  BarChartOutlined,
  LineChartOutlined,
  PieChartOutlined,
  AreaChartOutlined,
  UserOutlined,
  TransactionOutlined,
  ShopOutlined,
  FileTextOutlined,
  MoreOutlined
} from '@ant-design/icons-vue'

// 格式化数字
const formatNumber = (num: number): string => {
  return num.toLocaleString('zh-CN')
}
</script>

<style scoped>
.charts-demo {
  max-width: 1200px;
  margin: 0 auto;
}

.example-section {
  margin-bottom: 48px;
}

.section-title {
  font-size: 20px;
  font-weight: 600;
  color: #262626;
  margin: 0 0 8px 0;
}

.section-description {
  font-size: 14px;
  color: #8c8c8c;
  margin: 0 0 24px 0;
  line-height: 1.6;
}

/* 图表网格 */
.charts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 24px;
  margin-bottom: 32px;
}

.chart-card {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  transition: all 0.3s ease;
}

.chart-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.chart-header h4 {
  font-size: 16px;
  font-weight: 600;
  color: #262626;
  margin: 0;
}

.chart-period {
  font-size: 12px;
  color: #8c8c8c;
}

.chart-content {
  min-height: 200px;
}

.chart-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.chart-icon {
  font-size: 48px;
  color: #d9d9d9;
  margin-bottom: 16px;
}

.chart-text {
  font-size: 14px;
  color: #8c8c8c;
  margin-bottom: 20px;
}

/* 柱状图数据 */
.chart-data {
  width: 100%;
  max-width: 250px;
}

.data-item {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.data-label {
  width: 30px;
  font-size: 12px;
  color: #8c8c8c;
}

.data-bar {
  height: 8px;
  background: linear-gradient(90deg, #1890ff, #36cfc9);
  border-radius: 4px;
  flex: 1;
}

.data-value {
  font-size: 12px;
  font-weight: 500;
  color: #262626;
  width: 80px;
  text-align: right;
}

/* 折线图 */
.trend-chart {
  width: 100%;
  max-width: 300px;
}

/* 饼图 */
.pie-chart {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
  max-width: 200px;
}

.pie-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.pie-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.pie-label {
  flex: 1;
  font-size: 12px;
  color: #595959;
}

.pie-value {
  font-size: 12px;
  font-weight: 500;
  color: #262626;
}

/* 指标网格 */
.metrics-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  width: 100%;
  max-width: 200px;
}

.metric-item {
  text-align: center;
}

.metric-value {
  font-size: 20px;
  font-weight: 700;
  color: #262626;
  margin-bottom: 4px;
}

.metric-label {
  font-size: 12px;
  color: #8c8c8c;
  margin-bottom: 4px;
}

.metric-trend {
  font-size: 11px;
  font-weight: 500;
}

.metric-trend.positive {
  color: #52c41a;
}

/* 仪表盘网格 */
.dashboard-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
  gap: 24px;
}

.dashboard-card {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.dashboard-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.dashboard-header h4 {
  font-size: 16px;
  font-weight: 600;
  color: #262626;
  margin: 0;
}

/* 仪表盘内容 */
.gauge-container {
  display: flex;
  justify-content: space-around;
  align-items: center;
  gap: 24px;
}

.gauge-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.gauge-title {
  font-size: 12px;
  color: #8c8c8c;
  text-align: center;
}

.gauge-value {
  font-size: 16px;
  font-weight: 600;
  color: #262626;
}

/* KPI网格 */
.kpi-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

.kpi-item {
  display: flex;
  align-items: center;
  gap: 12px;
}

.kpi-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  background: linear-gradient(135deg, #1890ff, #36cfc9);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 18px;
}

.kpi-content {
  flex: 1;
}

.kpi-value {
  font-size: 18px;
  font-weight: 700;
  color: #262626;
  margin-bottom: 2px;
}

.kpi-label {
  font-size: 12px;
  color: #8c8c8c;
  margin-bottom: 2px;
}

.kpi-change {
  font-size: 11px;
  font-weight: 500;
}

.kpi-change.positive {
  color: #52c41a;
}

.kpi-change.negative {
  color: #ff4d4f;
}

/* 代码示例 */
.code-block {
  background: #f6f8fa;
  border: 1px solid #e1e4e8;
  border-radius: 6px;
  padding: 16px;
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  font-size: 13px;
  line-height: 1.45;
  overflow-x: auto;
  margin: 0;
}

.code-block code {
  background: none;
  padding: 0;
  font-size: inherit;
  color: #24292e;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .charts-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }

  .dashboard-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }

  .gauge-container {
    flex-direction: column;
    gap: 16px;
  }

  .kpi-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }

  .chart-card,
  .dashboard-card {
    padding: 20px;
  }
}
</style>